package com.weijian.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijian.common.pojo.CommonResult;
import com.weijian.common.pojo.PageResponse;
import com.weijian.dal.entity.SetMeal;
import com.weijian.dal.query.SetMealPageQuery;
import com.weijian.service.SetMealService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Schema(description = "套餐相关接口")
@Slf4j
@RequestMapping("/setmeal")
@RequiredArgsConstructor
public class SetMealController {

    final SetMealService setMealService;

    @Operation(summary = "分页查询套餐")
    @PostMapping("/list")
    public PageResponse<SetMeal> querySetMealByPage(@RequestBody SetMealPageQuery query) {
        Page<SetMeal> setMealPage = setMealService.lambdaQuery()
                .like(StrUtil.isNotBlank(query.getKey()), SetMeal::getName, query.getKey())
                .eq(SetMeal::getStatus, 1)
                .between(query.getMaxPrice() != null, SetMeal::getPrice, query.getMinPrice(), query.getMaxPrice())
                .page(query.toMpPage("update_time", false));
        return PageResponse.of(setMealPage, SetMeal.class);
    }

    @Operation(summary = "根据id批量查询套餐")
    @GetMapping("/gets")
    public CommonResult<List<SetMeal>> querySetMealByIds(@RequestParam("ids") List<Long> ids) {
        return setMealService.querySetMealByIds(ids);
    }

    @Operation(summary = "根据id查询套餐")
    @GetMapping("/get")
    public CommonResult<SetMeal> querySetMealById(@RequestParam("id") Long id) {
        return setMealService.querySetMealById(id);
    }

    @Operation(summary = "新增套餐")
    @PostMapping("/create")
    public void saveSetMeal(@RequestBody SetMeal SetMeal) {
        setMealService.save(SetMeal);
    }

    @Operation(summary = "更新套餐状态")
    @PutMapping("/status/{id}/{status}")
    public void updateSetMealStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        SetMeal SetMeal = new SetMeal();
        SetMeal.setId(id);
        SetMeal.setStatus(status);
        setMealService.updateById(SetMeal);
    }

    @Operation(summary = "更新套餐")
    @PutMapping("/update")
    public void updateSetMeal(@RequestBody SetMeal SetMeal) {
        // 不允许修改套餐状态，所以强制设置为null，更新时，就会忽略该字段
        SetMeal.setStatus(null);
        // 更新
        setMealService.updateSetMeal(SetMeal);
    }

    @Operation(summary = "根据id删除套餐")
    @DeleteMapping("/delete")
    public void deleteSetMealById(@RequestParam("id") Long id) {
        setMealService.deleteSetMealById(id);
    }

    @Operation(summary = "批量删除套餐")
    @DeleteMapping("/deletes")
    public void deleteSetMealByIds(@RequestParam("ids") List<Long> ids) {
        setMealService.deleteSetMealByIds(ids);
    }

    @Operation(summary = "根据分类id获取菜品集合")
    @GetMapping("/categoryId")
    public CommonResult<List<SetMeal>> listByCategoryId(@RequestParam("categoryId") Long categoryId) {
        return setMealService.listByCategoryId(categoryId);
    }

}
